import java.util.*;

public class PaperList {
    private final String name;
    private final List<Paper> papers;
    private final PaperSelection selection = new PaperSelection();
    private final Random random = new Random();

    public PaperList(String name, List<Paper> papers) {
        this.name = name;
        this.papers = papers;
    }

    public void selectPaperByRandom(Student student) {
        if (selection.hasSelected(student)) return;

        List<Paper> candidates = new ArrayList<>();
        for (Paper paper : papers) {
            if (student.getPreferredType() == PaperType.Any ||
                    paper.getType() == student.getPreferredType()) {
                candidates.add(paper);
            }
        }

        Collections.shuffle(candidates, random);

        List<Paper> selected = new ArrayList<>();
        for (Paper paper : candidates) {
            if (selected.size() >= student.getNumPreferred()) break;
            if (paper.select()) {
                selected.add(paper);
            }
        }

        if (!selected.isEmpty()) {
            selection.select(student, selected);
        }
    }

    public PaperSelection getSelectionResult() {
        return selection;
    }
}
